package cn.life.business.material.controller;

import cn.life.business.material.entity.po.MaterialPo;
import cn.life.business.material.entity.po.MaterialRecPo;
import cn.life.business.material.entity.po.MaterialTypePo;
import cn.life.business.material.entity.vo.MaterialRecVo;
import cn.life.business.material.service.MaterialService;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description:    提料单Controller
 *
 * @Author 李佳明
 * @Date 2018-07-26 10:16
 */
@RestController
@RequestMapping("material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    /**
     * @Description:    增加提料单信息
     *
     * @param materialRecVo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("record/add")
    public CMessage addMaterialRecord(@RequestBody MaterialRecVo materialRecVo, @MyUser MyUserInfo myUserInfo) throws Exception {
        setMaterialOprDate(materialRecVo,myUserInfo);
        return materialService.addMaterialRecord(materialRecVo);
    }

    /**
     * @Description:    修改提料单信息
     *
     * @param materialRecVo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("record/update")
    public CMessage updateMaterialRecord(@RequestBody MaterialRecVo materialRecVo, @MyUser MyUserInfo myUserInfo) throws Exception {
        if(materialRecVo.getNextUserId() == null){
            return CMessage.fail("审核人信息不可为空");
        }
        setMaterialOprDate(materialRecVo,myUserInfo);
        return materialService.updateMaterialRecord(materialRecVo);
    }

    /**
     * @Description:    查询材料单列表
     *
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:51 
     */
    @PostMapping("record/queryList")
    public CMessage queryList(@MyUser MyUserInfo myUserInfo,@RequestBody MaterialRecVo materialRecVo){
        if(materialRecVo.getProjectId() == null){
            materialRecVo.setProjectId(myUserInfo.getProjectId());
        }
        return materialService.queryRecordList(myUserInfo,materialRecVo);
    }

    /**
     * @Description:    查询材料单详情
     *
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:51
     */
    @PostMapping("record/queryDetail")
    public CMessage queryDetail(@RequestBody MaterialRecVo materialRecVo){
        return materialService.queryRecordDetail(materialRecVo);
    }

    /**
     * @Description:    审核提料单信息
     *
     * @param materialRecPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("record/audit")
    public CMessage auditMaterial(@RequestBody MaterialRecPo materialRecPo, @MyUser MyUserInfo myUserInfo){
        if(materialRecPo.getState() == 2){//审核通过
            materialRecPo.setAuditState(9);
        }else if(materialRecPo.getState() == 3){//审核不通过
            materialRecPo.setAuditState(0);
        }
        materialRecPo.setOprUserId(myUserInfo.getUserId());
        return materialService.auditMaterialRecord(materialRecPo);
    }

    /**
     * @Description:    增加材料信息
     *
     * @param materialPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("add")
    public CMessage addMaterial(@RequestBody MaterialPo materialPo, @MyUser MyUserInfo myUserInfo){
        setMaterialOprDate(materialPo,myUserInfo);
        return materialService.addMaterial(materialPo);
    }

    /**
     * @Description:    修改材料信息
     *
     * @param materialPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("update")
    public CMessage updateMaterial(@RequestBody MaterialPo materialPo){
        return materialService.updateMaterial(materialPo);
    }

    /**
     * @Description:    删除材料信息
     *
     * @param materialPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("delete")
    public CMessage deleteMaterial(@RequestBody MaterialPo materialPo){
        return materialService.deleteMaterial(materialPo);
    }

    /**
     * @Description:    材料信息列表
     *
     * @param materialPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("queryList")
    public CMessage queryMaterialList(@RequestBody MaterialPo materialPo){
        return materialService.queryMaterialList(materialPo);
    }

    /**
     * @Description:    材料信息详情
     *
     * @param materialPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("queryDetail")
    public CMessage queryMaterialDetail(@RequestBody MaterialPo materialPo){
        return materialService.queryMaterialDetail(materialPo);
    }


    /**
     * @Description:    增加材料类型信息
     *
     * @param materialTypePo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("type/add")
    public CMessage addMaterialType(@RequestBody MaterialTypePo materialTypePo, @MyUser MyUserInfo myUserInfo){
        setMaterialOprDate(materialTypePo,myUserInfo);
        return materialService.addMaterialType(materialTypePo);
    }

    /**
     * @Description:    修改材料类型信息
     *
     * @param materialTypePo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("type/update")
    public CMessage updateMaterialType(@RequestBody MaterialTypePo materialTypePo){
        return materialService.updateMaterialType(materialTypePo);
    }

    /**
     * @Description:    删除材料类型信息
     *
     * @param materialTypePo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("type/delete")
    public CMessage deleteMaterialType(@RequestBody MaterialTypePo materialTypePo){
        return materialService.deleteMaterialType(materialTypePo);
    }

    /**
     * @Description:    材料类型信息列表
     *
     * @param materialTypePo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("type/queryList")
    public CMessage queryMaterialTypeList(@RequestBody MaterialTypePo materialTypePo){
        return materialService.queryMaterialTypeList(materialTypePo);
    }

    /**
     * @Description:    材料类型信息详情
     *
     * @param materialTypePo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/26 0026 下午 1:46
     */
    @PostMapping("type/queryDetail")
    public CMessage queryMaterialTypeDetail(@RequestBody MaterialTypePo materialTypePo){
        return materialService.queryMaterialTypeDetail(materialTypePo);
    }

    public void setMaterialOprDate(MaterialRecVo materialRecVo, MyUserInfo myUserInfo){
        materialRecVo.setOprUserId(myUserInfo.getUserId());
        materialRecVo.setOprUserName(myUserInfo.getUserName());
        materialRecVo.setOprDate(new Date());
    }

    public void setMaterialOprDate(MaterialPo materialPo, MyUserInfo myUserInfo){
        materialPo.setOprUserId(myUserInfo.getUserId());
        materialPo.setOprUserName(myUserInfo.getUserName());
        materialPo.setOprDate(new Date());
    }

    public void setMaterialOprDate(MaterialTypePo materialTypePo, MyUserInfo myUserInfo){
        materialTypePo.setOprUserId(myUserInfo.getUserId());
        materialTypePo.setOprUserName(myUserInfo.getUserName());
        materialTypePo.setOprDate(new Date());
    }
}
